<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入计划名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['mes:dv:track:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trackList" @selection-change="handleSelectionChange">
      <el-table-column label="计划编码" align="center" prop="planCode" />
      <el-table-column label="计划名称" align="center" prop="planName" />
      <el-table-column label="计划类型" align="center" width="120px" prop="planType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dv_plan_type" :value="scope.row.planType" />
        </template>
      </el-table-column>
      <el-table-column label="月份" align="center" prop="month" width="90">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.month, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_plan_track_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="handleName" />
      <el-table-column label="处理时间" align="center" prop="handleTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跟踪情况" align="center" prop="explain" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleData(scope.row)" v-if="scope.row.isShow == 1">处理</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="showFile(scope.row)" v-if="scope.row.status =='2'" v-hasPermi="['mes:maintainPlan:appendix']">附件预览</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改设备点检保养计划跟踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编码" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请输入计划编码" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划名称" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划类型" prop="planType">
              <el-select v-model="form.planType" placeholder="请选择计划类型" disabled>
                <el-option v-for="dict in dict.type.dv_plan_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件上传">
              <FileUpload :limit="999" :value="form.fileList" :fileSize="15" :fileType="fileType" @input="handleImgUplaoded" @onRemoved="handleImgRemoved"></FileUpload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="跟踪情况" prop="explain">
              <el-input v-model="form.explain" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title1" :visible.sync="open2" width="40%" append-to-body>
      <el-table v-loading="false" :data="fileList">
        <el-table-column label="文件名称" align="center" prop="fileName" />
        <el-table-column label="附件列" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="openFile(scope.row)">查看附件</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog :title="title1" :visible.sync="open1" width="80%" append-to-body>
      <div slot="footer" class="dialog-footer">
        <vue-office-pdf v-if="showFileType=='pdf'" :src="dateUrl" style="width: 100%; height: 600px;" @rendered="renderedHandler" @error="errorHandler" />
        <div v-if="showFileType=='png'||  showFileType=='jpg' || showFileType=='jpeg' || showFileType=='PNG' || showFileType=='JPG' || showFileType=='JPEG'">
          <el-image @load="renderedHandler" @error="errorHandler" :src="dateUrl" style="width: 100%; height: 600px;" />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTrack, getTrack, handleTrack, selectFileManageList } from "@/api/mes/dv/track";
import FileUpload from "@/components/FileUpload/index.vue";
import FileView from "@/components/fileView/index.vue";
import VueOfficePdf from '@vue-office/pdf'
import { Message } from "element-ui";

export default {
  name: "Checkplantrack",
  dicts: ['check_plan_track_status', 'dv_plan_type'],
  components: { FileUpload, FileView, VueOfficePdf },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备点检保养计划跟踪表格数据
      trackList: [],
      fileList: [],
      // 弹出层标题
      title: "",
      title1: "附件预览",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      dateUrl: null,
      // 文件类型
      fileType: [
        "pdf", "png", "jpg", "jpeg", "PDF", "PNG", "JPG", "JPEG"
      ],
      showFileType: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        explain: [
          { required: true, message: "请输入跟踪情况", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备点检保养计划跟踪列表 */
    getList() {
      this.loading = true;
      listTrack(this.queryParams).then(response => {
        this.trackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planId: null,
        planCode: null,
        planName: null,
        planType: null,
        month: null,
        explain: null,
        files: null,
        status: null,
        handleId: null,
        handleName: null,
        handleTime: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        fileList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleData(row) {
      this.reset();
      getTrack(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "跟踪处理";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('DvCheckPlanTrack/track/export', {
        ...this.queryParams
      }, `track_${new Date().getTime()}.xlsx`)
    },
    //上传成功
    handleImgUplaoded(url) {
      let list = url.split(",")
      this.form.fileList = [];
      for (let i = 0; i < list.length; i++) {
        const element = {
          type: 3,
          url: list[i]
        };
        this.form.fileList.push(element);
      }
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      console.log(imgUrl);
      this.form.fileList = imgUrl;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!Array.isArray(this.form.fileList)) {
            this.handleImgUplaoded(this.form.fileList)
          }
          handleTrack(this.form).then(response => {
            this.$modal.msgSuccess("处理成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    showFile(row) {
      const param = {
        type: 3,
        relevanceId: row.id
      }
      selectFileManageList(param).then(response => {
        this.open2 = true;
        this.fileList = response.data
      });
    },
    openFile(row) {
      this.open1 = true;
      this.showFileType = row.fileName.substring(row.fileName.lastIndexOf('.') + 1)
      this.dateUrl = row.url;
    },
    renderedHandler() {
      console.log("渲染完成111111111111111");
      //   downloadLoadingInstance.close();
    },
    errorHandler(e, data) {
      console.log(e, data, "渲染失败222222222222");
      if (this.url !== "") {
        Message.error("渲染失败！");
      }
      //   downloadLoadingInstance.close();
    },
  }
};
</script>
